package com.webull.information.center.validator;

import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Component;

import com.webull.information.center.common.constants.CenterCode;
import com.webull.information.client.model.NewsLabel;
import com.webull.information.client.model.parm.NewsLabelParam;

/**
 * 新闻标签校验.
 *
 * @author longyang
 * @date 2016年6月27日 下午4:04:58
 * @version 1.0
 * @since JDK 1.8
 */
@Component
public class NewsLabelValidator extends BaseValidator {

	protected final Logger logger = LogManager.getLogger(getClass());

	public void validateAdd(NewsLabel label) {
		validateNewsLabel(label);
	}

	public void validateDel(Integer id) {
		validateId(id);
	}

	public void validateUpdate(NewsLabel label) {
		validateObjectIsNull(label, CenterCode.PARAM_NULL, "newsLabelObj");
		validateId(label.getId());
		validateNewsLabel(label);
	}

	private void validateNewsLabel(NewsLabel label) {
		validateObjectIsNull(label, CenterCode.PARAM_NULL, "newsLabelObj");
		validateStrTypeIsNull(label.getName(), CenterCode.PARAM_NULL, "name");
		validateStrTypeOverLimit(label.getName(), 10, CenterCode.PARAM_ERROR, "name");
		if (StringUtils.isNotBlank(label.getName())) {
			validateStrTypeOverLimit(label.getDescribe(), 50, CenterCode.DB_ERROR, "name");
			NewsLabelParam newsLabelParam = new NewsLabelParam();
			newsLabelParam.setName(label.getName());
			// List<NewsLabel> labels = newsLabelDao.paging(newsLabelParam);
			// if (labels != null && labels.size() > 0) {
			// throw CenterExceptionUtil.createCenterException(CenterCode.OBJECT_EXSIST, "NewsLabel");
			// }
		}
		if (label.getRegionId() != null) {
			validateId(label.getRegionId());
		}
	}

	private void validateId(Integer id) {
		validateIntegerTypeIsNull(id, CenterCode.PARAM_NULL, "id");
		validateIntegerTypeIsError(id, CenterCode.PARAM_ERROR, "id");
	}

}
